/*
 * tcp.cc
 *
 *  Created on: Dec 16, 2014
 *      Author: root
 */

#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include "tcp.h"

bool Tcp::IsACK() {
	return this->ack == 0x01;
}
bool Tcp::IsPSH() {
	return this->psh == 0x01;
}
bool Tcp::IsSYN() {
	return this->syn == 0x01;
}
bool Tcp::IsFIN() {
	return this->fin == 0x01;
}
bool Tcp::IsURG() {
	return this->urg == 0x01;
}
bool Tcp::IsRST() {
	return this->rst == 0x01;
}
char* Tcp::ToString() {
	static char info[1024] = { 0 };
	sprintf(info, "TCP %d, %d, %d:%d ", this->doff * 4, GetTcpPayloadLen(),
			ntohs(this->dest), ntohs(this->source));
	IsSYN() ? sprintf(info + strlen(info), " Is SYN ") : 1;
	IsPSH() ? sprintf(info + strlen(info), " Is PSH ") : 1;
	IsURG() ? sprintf(info + strlen(info), " Is URG ") : 1;
	IsACK() ? sprintf(info + strlen(info), " Is ACK ") : 1;
	IsFIN() ? sprintf(info + strlen(info), " Is FIN ") : 1;
	IsRST() ? sprintf(info + strlen(info), " Is RST ") : 1;
	printf("%s\n", info);
	return info;
}
unsigned char* Tcp::GetTcpPayload() {
	return (unsigned char*) this + this->doff * 4;
}
unsigned int Tcp::GetTcpPayloadLen() {
	return this->urg_ptr - this->doff * 4;
}
